package com.feign.config;


import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Configuration
public class FeignConfiguration implements RequestInterceptor {

    /**
     * Spring Cloud OpenFeign远程调用不能传递请求头(Header)解决方法
     *
     * @param template
     */
    @Override
    public void apply(RequestTemplate template) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (null != attributes) {
            System.out.println("FeignConfiguration");
            HttpServletRequest request = attributes.getRequest();
            template.header("token", request.getHeader("token"));
        }
    }

}
